﻿@model List<CustomerActionTypeModel>
@{  
    //page title
    ViewBag.Title = T("Admin.Customers.ActionType").Text;
}
<form asp-controller="CustomerActionType" asp-action="SaveTypes" method="post">
    
    <div class="row">
        <div class="col-md-12">
            <div class="x_panel light form-fit">
                <div class="x_title">
                    <div class="caption">
                        <i class="fa fa-repeat"></i>
                        @T("Admin.Customers.ActionType")
                    </div>
                    <div class="actions">
                        <button class="btn btn-success" type="submit" name="save"><i class="fa fa-check"></i> @T("Admin.Common.Save") </button>
                    </div>
                </div>
                <div class="x_content form">
                    <div id="actionType-grid"></div>
                </div>
            </div>
        </div>
    </div>
    <script>

        //local datasource
        var actionTypes = [
            @for(int i=0;i<Model.Count;i++)
        {
            var alt = Model[i];
            <text>
            {
                Id: "@(alt.Id)",
                Name: "@(Html.Raw(System.Text.Encodings.Web.JavaScriptEncoder.Default.Encode(alt.Name)))",
                Enabled: @(alt.Enabled.ToString().ToLowerInvariant())
            }
        </text>
            if (i != Model.Count - 1)
            {
                <text>,</text>
            }
        }
        ];

        $(document).ready(function () {
            $("#actionType-grid").kendoGrid({
                dataSource: {
                    data: actionTypes,
                    schema: {
                        model: {
                            fields: {
                                Id: { type: "string" },
                                Name: { type: "string" },
                                Enabled: { type: "boolean" }
                            }
                        }
                    },
                    error: function (e) {
                        display_kendoui_grid_error(e);
                        // Cancel the changes
                        this.cancelChanges();
                    }
                },
                pageable: {
                    refresh: true,
                    numeric: false,
                    previousNext: false,
                    info: false
                },
                scrollable: false,
                columns: [
                    {
                        field: "Name",
                        title: "@T("Admin.Customers.ActionType.Fields.Name")",
                        width: 100
                    }, {
                        field: "Enabled",
                        headerTemplate: "@T("Admin.Customers.ActionType.Fields.Enabled")&nbsp<label class='mt-checkbox mt-checkbox-outline control control-checkbox'><input id='mastercheckbox' type='checkbox' /> <div class='control__indicator'></div></label>",
                        headerAttributes: { style: "text-align:center" },
                        attributes: { style: "text-align:center" },
                        template: "<label class='mt-checkbox mt-checkbox-outline control control-checkbox'><input name='checkbox_action_types' value='#=Id#' type='checkbox' class='checkboxGroups' # if(Enabled) {# checked='checked' #}  # /> <div class='control__indicator'></div></label>",
                        width: 100
                    }
                ]
            });
        });

    </script>
                

    <script type="text/javascript">

        $(document).ready(function () {

            updateMasterCheckbox();

            $('#mastercheckbox').click(function () {
                $('.checkboxGroups').prop('checked', $(this).is(':checked')).change();
            });

            //wire up checkboxes.
            $('#actionType-grid').on('change', 'input[type=checkbox][id!=mastercheckbox]', function (e) {
                updateMasterCheckbox();
            });
        });

        function updateMasterCheckbox() {
            var numChkBoxes = $('#actionType-grid input[type=checkbox][id!=mastercheckbox]').length;
            var numChkBoxesChecked = $('#actionType-grid input[type=checkbox][id!=mastercheckbox]:checked').length;
            $('#mastercheckbox').prop('checked', numChkBoxes == numChkBoxesChecked && numChkBoxes > 0);
        }
    </script>   
</form>